package org.dromara.system.service.impl;

import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.system.domain.vo.DepartmentTreeVo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.DepartmentBo;
import org.dromara.system.domain.vo.DepartmentVo;
import org.dromara.system.domain.Department;
import org.dromara.system.mapper.DepartmentMapper;
import org.dromara.system.service.IDepartmentService;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 部门Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    private final DepartmentMapper baseMapper;

    /**
     * 查询部门
     *
     * @param id 主键
     * @return 部门
     */
    @Override
    public DepartmentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询部门列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 部门分页列表
     */
    @Override
    public TableDataInfo<DepartmentVo> queryPageList(DepartmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Department> lqw = buildQueryWrapper(bo);
        Page<DepartmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的部门列表
     *
     * @param bo 查询条件
     * @return 部门列表
     */
    @Override
    public List<DepartmentVo> queryList(DepartmentBo bo) {
        LambdaQueryWrapper<Department> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Department> buildQueryWrapper(DepartmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Department> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Department::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getOrgCode()), Department::getOrgCode, bo.getOrgCode());
        lqw.eq(StringUtils.isNotBlank(bo.getBmmc()), Department::getBmmc, bo.getBmmc());
        lqw.eq(StringUtils.isNotBlank(bo.getSjbh()), Department::getSjbh, bo.getSjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getBmfzr()), Department::getBmfzr, bo.getBmfzr());
        lqw.eq(StringUtils.isNotBlank(bo.getFzrdh()), Department::getFzrdh, bo.getFzrdh());
        lqw.eq(Department::getDelFlag, 0);
        return lqw;
    }

    /**
     * 新增部门
     *
     * @param bo 部门
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(DepartmentBo bo) {
        Department add = MapstructUtils.convert(bo, Department.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改部门
     *
     * @param bo 部门
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(DepartmentBo bo) {
        Department update = MapstructUtils.convert(bo, Department.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Department entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除部门信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public Boolean checkDepartmentExists(Long deptId) {
        return baseMapper.exists(new LambdaQueryWrapper<Department>().eq(Department::getId, deptId).eq(Department::getDelFlag, SystemConstants.NORMAL));
    }

    @Override
    public List<DepartmentTreeVo> getDeptTreeList() {
        List<Department> departments = baseMapper.selectList(
            Wrappers.<Department>lambdaQuery()
                .eq(Department::getDelFlag, 0)
                .orderByAsc(Department::getSjbh, Department::getId)
        );

        List<DepartmentTreeVo> convert = departments.stream().map(this::convertToTreeVo).toList();
        Map<String, DepartmentTreeVo> departmentMap = convert.stream()
            .collect(Collectors.toMap(vo -> String.valueOf(vo.getId()), Function.identity()));

        return departmentMap.values().stream()
            .filter(vo -> "01".equals(vo.getSjbh()))
            .peek(root -> buildTreeChildren(root, departmentMap))
            .collect(Collectors.toList());
    }

    private DepartmentTreeVo convertToTreeVo(Department dept) {
        DepartmentTreeVo vo = new DepartmentTreeVo();
        vo.setId(dept.getId());
        vo.setOrgCode(dept.getOrgCode());
        vo.setBmmc(dept.getBmmc());
        vo.setSjbh(dept.getSjbh());
        vo.setBmfzr(dept.getBmfzr());
        vo.setFzrdh(dept.getFzrdh());
        return vo;
    }

    /**
     * 递归构建子部门树
     */
    private void buildTreeChildren(DepartmentTreeVo parent, Map<String, DepartmentTreeVo> departmentMap) {
        List<DepartmentTreeVo> children = departmentMap.values().stream()
            .filter(vo -> String.valueOf(parent.getId()).equals(vo.getSjbh()))
            .peek(child -> buildTreeChildren(child, departmentMap))
            .collect(Collectors.toList());

        if (!children.isEmpty()) {
            children.sort(Comparator.comparing(DepartmentTreeVo::getId));
            parent.setChildren(children);
        }
    }
}
